﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Buccaneer;

namespace Buccontrols
{
    public partial class PluginForm : Form
    {
        public PluginForm()
        {
            InitializeComponent();
        }

        public PluginCouple ParentCouple;

        private string exclude_list = "";
        private PluginDaddy daddy;


        public void PluginForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Middle)
            {
                (this.Daddy as PluginDaddy).Plugins.Act("startdragging", new string[] 
                {
                    this.Name
                });
            }
            this.Refresh();
            this.Update();
            this.PerformLayout();
        }

        private void PluginForm_Shown(object sender, EventArgs e)
        {
            if (this.Controls.Count == 0)
            {
                Button btn = new Button();
                btn.Text = "Load plugin!";
                btn.Name = "loadPlugin";
                btn.Dock = DockStyle.Fill;
                btn.Click += new EventHandler(btn_Click);
                this.Controls.Add(btn);
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Choose a plugin to load";
            ofd.InitialDirectory = Options.PluginsFolder;
            ofd.Filter = "Buccaneer Plugin | *.dll | Buccaneer Plugin Package | *.bucc.pip";
            ofd.ShowDialog();
        }

        #region Overriden Functions

        public void Control_ControlAdded(object sender, ControlEventArgs e)
        {
            //
            //Add Mouse Event Handlers for each control added into the form,
            //if Draggable property of the form is set to true and the control
            //name is not in the ExcludeList.Exclude list is the comma separated
            //list of the Controls for which you do not require the mouse handler 
            //to be added. For Example a button.  
            //
            if ((this.ExcludeList.IndexOf(e.Control.Name) == -1))
            {
                if (e.Control.GetType() == typeof(Panel) || e.Control.GetType() == typeof(SplitContainer))
                {
                    AddMouseClickToPanel(e.Control);
                }
                else
                {
                    e.Control.MouseDown += new MouseEventHandler(PluginForm_MouseDown);
                }
            }
        }

        public void AddMouseClickToPanel(Control panel)
        {
            if (panel.GetType() == typeof(Panel))
            {
                foreach (Control c in panel.Controls)
                {
                    AddMouseClickToPanel(c);
                }
                panel.ControlAdded += new ControlEventHandler(Control_ControlAdded);
            }
            if (panel.GetType() == typeof(SplitContainer))
            {
                foreach (Control c in (panel as SplitContainer).Panel1.Controls)
                {
                    AddMouseClickToPanel(c);
                }
                foreach (Control c in (panel as SplitContainer).Panel2.Controls)
                {
                    AddMouseClickToPanel(c);
                }
                panel.ControlAdded += new ControlEventHandler(Control_ControlAdded);
            }
            else
            {
                panel.MouseDown += new MouseEventHandler(PluginForm_MouseDown);
            }
        }

        #endregion

        #region Properties

        public string ExcludeList
        {
            set
            {
                this.exclude_list = value;
            }
            get
            {
                return this.exclude_list.Trim();
            }
        }

        public PluginDaddy Daddy
        {
            get { return this.daddy; }
            set { this.daddy = value; }
        }

        #endregion

        private void PluginForm_Activated(object sender, EventArgs e)
        {
            this.Daddy.Show();
            (this.Daddy as PluginDaddy).Plugins.Act("activatePlugin", new string[]
            {
                this.Name
            });
        }

        private void PluginForm_Deactivated(object sender, EventArgs e)
        {
            if (this.Daddy.Active) return;
            this.TopMost = false;
        }

        #region Public Functions

        public void Act(string action, string[] args)
        {
            (this.Daddy as PluginDaddy).Plugins.Act(action, args);
        }

        #endregion

        private void PluginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            /*/
            (this.Daddy as PluginDaddy).Plugins.Act("unloadplugin", new string[]
            {
                this.Name
            });
            //*/
        }

        private void PluginForm_Load(object sender, EventArgs e)
        {
            
        }

    }
}